package com.financing.wap.dao.impl;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.financing.wap.common.Pager;
import com.financing.wap.common.SqlName;
import com.financing.wap.dao.IProductDao;
import com.financing.wap.dto.FpProjectRabate;
import com.financing.wap.dto.MailRecordDTO;
import com.financing.wap.dto.ProductAnnualRevenueDTO;
import com.financing.wap.dto.ProductAttachmentDTO;
import com.financing.wap.dto.ProductBasicInfoDTO;
import com.financing.wap.dto.ProductDTO;
import com.financing.wap.dto.ProductDetailPageDTO;
import com.financing.wap.dto.ProductRecommendDTO;
import com.financing.wap.dto.ProjectAccountDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.util.StringUtil;

@Repository("productDao")
@SuppressWarnings(value={"unchecked", "deprecation"})
public class ProductDaoImpl extends BaseDaoImpl<ProductDTO, Integer> implements IProductDao {

	@Override
	public List<ProductDTO> getProductList(Map<String,Object> queryParams, UserDTO user, Pager pager) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("queryParams", queryParams);
		param.put("user", user);
		param.put("pager", pager);
		Object isnewCount = queryParams.get("isnewCount");
		if(isnewCount==null)
			return (List<ProductDTO>) super.sqlMapClientTemplate.queryForList( SqlName.PRODCUT_QUERY_LIST, param);
		else 
			return (List<ProductDTO>) super.sqlMapClientTemplate.queryForList( SqlName.PRODCUT_QUERY_LISTN, param);
	}

	@Override
	public Class<ProductDTO> getEntityClass() {
		return ProductDTO.class;
	}

	@Override
	public List<ProductDetailPageDTO> getRecommendProduct(Map<String,Object> parames) {
		List<ProductDetailPageDTO> recommendList = this.sqlMapClientTemplate.queryForList("product.getRecommendProduct", parames);
		if(!recommendList.isEmpty()){
			return recommendList;
		}
		return null;
	}

	@Override
	public ProductDetailPageDTO getLatestProduct() {
		List<ProductDetailPageDTO> recommendList = this.sqlMapClientTemplate.queryForList("product.getLatestProduct", null);
		if(!recommendList.isEmpty()){
			return recommendList.get(0);
		}
		return null;
	}

	@Override
	public ProductDetailPageDTO getProductDetailPage(Integer productId) {
		return (ProductDetailPageDTO)this.sqlMapClientTemplate.queryForObject("product.getProductDetailPage", productId);
	}

	@Override
	public List<FpProjectRabate> getProductProfitRebateList(Integer productId) {
		return this.sqlMapClientTemplate.queryForList("product.getProductProfitRebateList", productId);
	}

	@Override
	public List<Map<String, Object>> getProductOrderStat(Integer productId) {
		return this.sqlMapClientTemplate.queryForList("product.getProductOrderStat", productId);
	}
	
	public Integer getProductIdByProductCode(String productCode){
		return (Integer)this.sqlMapClientTemplate.queryForObject("product.getProductIdByProductCode", productCode);
	}

	@Override
	public int getProductListCount(Map<String,Object> queryParams, UserDTO user) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("queryParams", queryParams);
		param.put("user", user);
		Object isnewCount = queryParams.get("isnewCount");
		if(isnewCount==null)
			return (Integer) super.sqlMapClientTemplate.queryForObject( SqlName.PRODCUT_QUERY_LIST_COUNT, param);
		else 
			return (Integer) super.sqlMapClientTemplate.queryForObject( SqlName.PRODCUT_QUERY_LIST_COUNTN, param);
	}

	@Override
	public ProductAnnualRevenueDTO queryCurrentAnnualRevenue(int id,String sort) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", id);
		params.put("sort", sort);
		return (ProductAnnualRevenueDTO) super.sqlMapClientTemplate.queryForObject("product.queryAnnualRevenue", params);
	}

	@Override
	public Map<String,Object> queryProductBuyNumberAndAmount(Integer productId) {
		
		return (Map<String,Object>)this.sqlMapClientTemplate.queryForObject("product.queryProductBuyNumberAndAmount", productId);
	}
//
//	@Override
//	public BigDecimal queryProductBuyNumber(Integer productId) {
//		
//		return (BigDecimal)this.sqlMapClientTemplate.queryForObject("product.queryProductBuyAmount", productId);
//	}

	@Override
	public List<ProjectAccountDTO> queryProjectAccount(int projectId) {
		return this.sqlMapClientTemplate.queryForList("product.queryProjectAccount",projectId);
	}

	@Override
	public List<ProductAttachmentDTO> getProductAttachments(ProductAttachmentDTO  a) {
		return this.sqlMapClientTemplate.queryForList("product.queryProductAttachment",a);
	}
	
	@Override
	public Integer insertMailRecord(MailRecordDTO  mailRecordDTO) {
		return (Integer)this.sqlMapClientTemplate.insert("product.insertMailRecord",mailRecordDTO);
	}

	@Override
	public List<Map> querySiteMapProjectList() {
		return this.sqlMapClientTemplate.queryForList(SqlName.PRODCUT_SITEMAP_LIST);
	}

	@Override
	public List<Map> querySiteMapFundList() {
		return this.sqlMapClientTemplate.queryForList(SqlName.FUND_SITEMAP_LIST);
	}

	@Override
	public List<Map> querySiteMapActivitiesList() {
		return this.sqlMapClientTemplate.queryForList(SqlName.ACTIVITIES_SITEMAP_LIST);
	}
	
	@Override
	public List<Map> querySiteMapProjectCompanyList() {
		return this.sqlMapClientTemplate.queryForList(SqlName.PROJECTCOMPANY_SITEMAP_LIST);
	}

	@Override
	public ProductBasicInfoDTO queryBasicInfo(String code) {
		return (ProductBasicInfoDTO)this.sqlMapClientTemplate.queryForObject("product.queryBasicInfo", code);
	}

	@Override
	public Map<String, Integer> queryProjectAmountCount() {
		return (Map<String, Integer>) this.sqlMapClientTemplate.queryForObject("product.queryProjectAmountCount");
	}
	
	@Override
	public List<ProductDTO> getProductByPcode(Integer id){
		return this.sqlMapClientTemplate.queryForList(SqlName.queryProjectByPcode, id);
	}

	@Override
	public int getProductPEVCListCount(Map<String,Object> params) {
		return (Integer) super.sqlMapClientTemplate.queryForObject( SqlName.PRODCUT_PEVC_QUERY_LIST_COUNT, params);
	}

	@Override
	public List<ProductDTO> getProductPEVCList(Map<String, Object> params) {
		return this.sqlMapClientTemplate.queryForList(SqlName.PRODCUT_PEVC_QUERY_LIST, params);
	}

	@Override
	public List<ProductDTO> getProductName() {
		
		return this.sqlMapClientTemplate.queryForList(SqlName.QUERY_HEIGH_SEARCH_COUNT);
	}
	@Override
	public List<ProductDTO> getProductByType(Map<String, Object> queryParams) {
		return this.sqlMapClientTemplate.queryForList(SqlName.queryProductByType,queryParams);
	}
	
	@Override
	public int queryProductSubBypId(Map<String, Object> params) {
		return (Integer) super.sqlMapClientTemplate.queryForObject( SqlName.queryProductSubBypId, params);
	}

	@Override
	public List<Map<String, Object>> getMediaSourceProduct(
			Map<String, Object> map) throws SQLException {
		return this.sqlMapClientTemplate.queryForList(SqlName.getMediaSourceProduct, map);
	}

	@Override
	public List<ProductRecommendDTO> getRecommendFundProduct() {
		List<ProductRecommendDTO> recommendList = this.sqlMapClientTemplate.queryForList("product.getRecommendFundProduct");
		if(!recommendList.isEmpty()){
			return recommendList;
		}
		return null;
	}

	@Override
	public List<ProductDTO> getRecommendProductForProductInfo(Map<String, Object> map) throws SQLException {
		return this.sqlMapClientTemplate.queryForList("product.getRecommendProductForProductInfo",map);
	}

	@Override
	public ProductAnnualRevenueDTO queryAnnualRevenue(int id, String sort) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", id);
		params.put("sort", sort);
		//查询竞价
		ProductAnnualRevenueDTO pard = (ProductAnnualRevenueDTO) super.sqlMapClientTemplate.queryForObject("product.queryBiddingAnnualRevenue", params);
		if(pard==null||StringUtils.isBlank(pard.getExpectAnnualRevenue())){
			//查询当前
			pard=(ProductAnnualRevenueDTO) super.sqlMapClientTemplate.queryForObject("product.queryAnnualRevenue", params);
		}
		return pard;
	}

	@Override
	public ProductBasicInfoDTO queryAllProductBasicInfo(String code) {
		return (ProductBasicInfoDTO)this.sqlMapClientTemplate.queryForObject("product.queryAllProductBasicInfo", code);
	}

	@Override
	public String getProductNameByProductCode(String param) {
	
		return (String)this.sqlMapClientTemplate.queryForObject("product.getProductNameByProductCode", param);
	}

}
